package com.talkology.pilot.student.service.impl;

import com.talkology.pilot.database.dao.CreditManagerMapper;
import com.talkology.pilot.database.dao.CreditOpRecordMapper;
import com.talkology.pilot.database.model.CreditManager;
import com.talkology.pilot.database.model.CreditOpRecord;
import com.talkology.pilot.student.service.ICreditManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CreditManagerServiceImpl implements ICreditManagerService {
    private static final Logger LOG = LoggerFactory.getLogger(CreditManagerServiceImpl.class);

    @Autowired
    private CreditManagerMapper mapper;
    @Autowired
    private CreditOpRecordMapper recordMapper;

    @Override
    public List<CreditManager> getAll() {
        return mapper.selectAll();
    }

    @Override
    public CreditManager getInfo(Integer index) {
        return mapper.selectByPrimaryKey(index);
    }

    @Override
    @Transactional
    public void add(CreditManager record) {
        if (!record.getCreditLast().equals(record.getCreditTotal())) {
            throw new RuntimeException("Error input, creditLast must equal creditTotal when add.");
        }
        mapper.insert(record);

        //记录日志
        CreditOpRecord opRecord  = new CreditOpRecord(record);
        opRecord.setOperateType(1);
        opRecord.setChangeNum(record.getCreditLast());
        opRecord.setCreateAt(new Date());
        recordMapper.insert(opRecord);
        String studentId = record.getStudentId();
        Double num = record.getCreditTotal();
        LOG.info("Student index:{} changed credit {}.", studentId, num);
    }

    @Override
    @Transactional
    public void edit(CreditManager record) {
        mapper.updateByPrimaryKey(record);
    }

    @Override
    @Transactional
    public void delete(Integer index) {
        mapper.deleteByPrimaryKey(index);
    }

    @Override
    @Transactional
    public void changeCredit(String studentId, Double num) {
        CreditManager creditManager = mapper.selectByStudentId(studentId);
        if (num > 0) {
            creditManager.setCreditTotal(creditManager.getCreditTotal() + num);
        }

        //记录日志
        Integer operationType = num > 0 ? 1 : -1;
        CreditOpRecord opRecord  = new CreditOpRecord(creditManager);
        opRecord.setOperateType(operationType);
        opRecord.setChangeNum(num);
        opRecord.setCreateAt(new Date());
        recordMapper.insert(opRecord);
        Double creditLast = creditManager.getCreditLast() + num;
        if (num < 0 && creditLast < 0) {
            throw new RuntimeException("Not enough credit.");
        }
        creditManager.setCreditLast(creditLast);
        mapper.updateByPrimaryKey(creditManager);
        LOG.info("Student studentId:{} changed credit {}.", studentId, num);
    }

    @Override
    @Transactional
    public CreditManager selectByStudentId(String studentId) {
        return mapper.selectByStudentId(studentId);
    }
}
